<template>
  <div class="login-page">
    <div class="login-title">
      <h1>欢迎登陆</h1>
    </div>
    <van-form @submit="onSubmit">
      <van-cell-group>
        <van-field
          v-model="username"
          name="用户名"
          label="用户名"
          placeholder="请输入用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="密码"
          label="密码"
          placeholder="请输入密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <div style="margin: 16px">
        <van-button :loading="loading" round block type="primary" native-type="submit"> 登录 </van-button>
      </div>
    </van-form>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { postLogin } from '@/api';

import { useRouter } from 'vue-router';

import { showSuccessToast } from 'vant';

const router = useRouter();

const username = ref('');
const password = ref('');
const loading = ref(false);

const onSubmit = async values => {
  try {
    loading.value = true;
    let res = await postLogin(values);
    if (res) {
      loading.value = false;
      showSuccessToast('登录成功');
      router.push({ path: 'nav/home' });
    }
  } catch (error) {
    loading.value = false;
    console.log(error);
  }
};
</script>

<style lang="less" scoped>
.login-page {
  .login-title {
    height: 90px;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;

    h1 {
      font-size: 30px;
    }
  }
}
</style>
